using Microsoft.Data.Sqlite;

namespace 第八次作业
{
    public partial class Form1 : Form
    {
        private string connectionString = "Data Source=../../../sql/words.db;";
        private SqliteConnection connection;

        private string currentEnglishWord;
        private string currentChineseWord;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SQLitePCL.Batteries.Init();
            connection = new SqliteConnection(connectionString);
            connection.Open();

            // 显示第一个单词
            ShowNextWord();
        }

        private void ShowNextWord()
        {
            // 查询下一个单词
             var command = new SqliteCommand("SELECT english, chinese FROM words ORDER BY RANDOM() LIMIT 1", connection);
            // var command = new SqliteCommand(".tables;");
            var reader = command.ExecuteReader();
            if (reader.Read())
            {
                currentEnglishWord = reader.GetString(0);
                currentChineseWord = reader.GetString(1);

                // 显示中文词义
                ChineseText.Text = currentChineseWord;

                // 清空用户输入框
                EnglishText.Text = "";
                EnglishText.Focus();
            }
        }

        private void EnglishTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                // 比较用户输入的单词是否正确
                string input = EnglishText.Text.Trim();
                if (string.Equals(input, currentEnglishWord, StringComparison.OrdinalIgnoreCase))
                {
                    MessageBox.Show("正确");
                }
                else
                {
                    MessageBox.Show("错误");
                }

                // 显示下一个单词
                ShowNextWord();
            }
        }
    }
}